package com.afd.ozblip.activity;

import java.util.HashMap;
import java.util.Map;

import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AddFriendActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private LinearLayout sendVerifyLayout;
	private EditText verifyMsgEt;
	/****************** 数据变量 **************************/
	private String verifyMsgStr;
	private String userId;
	private Map<String, Object> resultMap;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_friend);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.sendVerifyLayout = (LinearLayout) findViewById(R.id.ll_user_sendVerify_btn);
		this.verifyMsgEt= (EditText) findViewById(R.id.et_add_friend_verifyMsg);
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_user_sendVerify_btn:
					verifyMsgStr = verifyMsgEt.getText().toString();
					if(!StringUtil.isNullOrEmpty(verifyMsgStr)){
						requestAddVipData();
					}else{
						showShortToast(AddFriendActivity.this,"请输入验证消息！",true);
					}
					break;

				default:
					break;
				}
			}
		};
		this.sendVerifyLayout.setOnClickListener(moreClick);
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		Intent intent = getIntent();
		this.userId = intent.getStringExtra("userId");
	}
	/**
	 * 获取添加VIP数据
	 */
	private void requestAddVipData() {
		if (commonUserInfo == null) {
			return;
		}
		AsyncLoadData ald = new AsyncLoadData(this, "提交中...",
				new AsyncAction() {

					@Override
					public void doLoad() throws Exception {
						// 获取提交参数
						Map<String, Object> addReviewParamMap = new HashMap<String, Object>();
						addReviewParamMap.put("userId",
								commonUserInfo.getUserId());
						addReviewParamMap.put("friendId", userId);
						addReviewParamMap.put("verifyMsg", verifyMsgStr);

						// 发送请求
						byte[] bUserFriend = HttpUtil.requestByPost(
								AddFriendActivity.this, Constants.USER_ADD_FRIEND_API,
								addReviewParamMap);
						if (bUserFriend != null) {
							resultMap = new ParseJson().parseAddResultJson(bUserFriend);
						}
					}

					@Override
					public void loadEnd() throws Exception {
						boolean returnFlg = (Boolean) resultMap.get("returnFlg");
						String returnMsg = resultMap.get("regMessage").toString();
						if (returnFlg) {
							showShortToast(AddFriendActivity.this,returnMsg,true);
							finish();
						}else{
							System.out.println(returnMsg);
						}
					}

				});
		ald.execute(0);

	}
}